package lib.self.utils;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;

import java.math.RoundingMode;
import java.text.NumberFormat;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import lib.self.AppEx;
import lib.self.util.autoFit.FitDpUtil;
import lib.self.util.res.ResLoader;

/**
 * description: 字符串工具类
 * author: Steven Xiao
 * date: 2017/5/25
 */
public class TextUtil {

    /**
     * 反转字符串
     */
    public static String getReversString(String text) {
        StringBuilder builder = new StringBuilder(text);
        return builder.reverse().toString();
    }

    /**
     * 将null转换成空字符串
     */
    public static String changeTextToEmptyIfNull(String text) {
        String result = "";
        if (TextUtils.isEmpty(text)) {
            return result;
        } else {
            return text;
        }
    }

    public static SpannableStringBuilder getSpanColor(String text, int startIndex, int endIndex, @ColorRes int color) {
        return getSpanColorSize(null, text, startIndex, endIndex, color, 0);
    }

    public static SpannableStringBuilder getSpanSize(String text, int startIndex, int endIndex, int sizeDp) {
        return getSpanColorSize(null, text, startIndex, endIndex, 0, sizeDp);
    }

    public static SpannableStringBuilder getSpanColorAndSize(String text, int startIndex, int endIndex, @ColorRes int color, int sizeDp) {
        return getSpanColorSize(null, text, startIndex, endIndex, color, sizeDp);
    }

    /**
     * 根据下标设置字体的颜色和大小
     *
     * @param startIndex 开始下标
     * @param endIndex   结束下标
     * @param color      颜色 @ColorRes
     * @param sizeDp     字体大小
     */
    public static SpannableStringBuilder getSpanColorSize(SpannableStringBuilder builder, String text, int startIndex, int endIndex, @ColorRes int color, int sizeDp) {
        SpannableStringBuilder spannable;
        if (null != builder) {
            spannable = builder;
        } else {
            spannable = new SpannableStringBuilder(text);
        }
        if (color != 0) {
            spannable.setSpan(new ForegroundColorSpan(ResLoader.getColor(color)), startIndex, endIndex + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (sizeDp != 0) {
            spannable.setSpan(new AbsoluteSizeSpan(FitDpUtil.dpToPx(sizeDp, AppEx.getCt())), startIndex, endIndex + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannable;
    }

    /**
     * @return 文字部分变颜色
     */
    public static SpannableStringBuilder getColorStringBuilder(@ColorInt int color, String text, int start, int end) {
        ForegroundColorSpan span = new ForegroundColorSpan(color);
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(text);
        stringBuilder.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return stringBuilder;
    }

    public static SpannableStringBuilder getColorStringBuilder(String text, String color, int start, int end) {
        return getColorStringBuilder(Color.parseColor(color), text, start, end);
    }

    public static SpannableStringBuilder getColorStringBuilder(String text, @ColorRes int colorRes, int start, int end) {
        return getColorStringBuilder(ResLoader.getColor(colorRes), text, start, end);
    }

    /**
     * 通过条件来截取字符串
     *
     * @param source
     * @param condition
     * @return
     */
    public static String subStringWithCondition(String source, String condition) {
        int index = source.indexOf(condition);
        return source.substring(0, index);
    }

    /**
     * 保留两位小数
     */
    public static String formatDouble(double d) {
        NumberFormat nf = NumberFormat.getNumberInstance();
        // 保留两位小数
        nf.setMaximumFractionDigits(2);
        // 如果不需要四舍五入，可以使用RoundingMode.DOWN
        nf.setRoundingMode(RoundingMode.UP);
        return nf.format(d);
    }

    /**
     * 去除double多余的0
     */
    public static String formatDouble(String content) {
        if (TextUtils.isEmpty(content)) {
            return "";
        }
        int length = content.length();
        String lastChar = content.substring(length - 1);
        if (content.contains(".00")) {
            int index = content.lastIndexOf(".");
            return content.substring(0, index);
        } else if (lastChar.equals(0)) {
            return content.substring(0, length - 1);
        } else {
            return content;
        }
    }

    /**
     * 字数到达最大值后 显示...
     */
    public static String ellipsisText(String content, int maxLength) {
        if (content.length() > maxLength) {
            return content.substring(0, maxLength - 1) + "…";
        } else {
            return content;
        }
    }
}
